package com.danciyixue.boot.dcyx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danciyixue.boot.dcyx.mapper.UnitsMapper;
import com.danciyixue.boot.dcyx.service.UnitsService;
import com.danciyixue.boot.dcyx.model.entity.Units;
import com.danciyixue.boot.dcyx.model.form.UnitsForm;
import com.danciyixue.boot.dcyx.model.query.UnitsQuery;
import com.danciyixue.boot.dcyx.model.vo.UnitsVO;
import com.danciyixue.boot.dcyx.converter.UnitsConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 单元信息服务实现类
 *
 * @author youlaitech
 * @since 2025-06-15 16:34
 */
@Service
@DS("dcyx")
@RequiredArgsConstructor
public class UnitsServiceImpl extends ServiceImpl<UnitsMapper, Units> implements UnitsService {

    private final UnitsConverter unitsConverter;

    /**
    * 获取单元信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UnitsVO>} 单元信息分页列表
    */
    @Override
    public IPage<UnitsVO> getUnitsPage(UnitsQuery queryParams) {
        Page<UnitsVO> pageVO = this.baseMapper.getUnitsPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取单元信息表单数据
     *
     * @param id 单元信息ID
     * @return 单元信息表单数据
     */
    @Override
    public UnitsForm getUnitsFormData(Long id) {
        Units entity = this.getById(id);
        return unitsConverter.toForm(entity);
    }
    
    /**
     * 新增单元信息
     *
     * @param formData 单元信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveUnits(UnitsForm formData) {
        Units entity = unitsConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新单元信息
     *
     * @param id   单元信息ID
     * @param formData 单元信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateUnits(Long id,UnitsForm formData) {
        Units entity = unitsConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除单元信息
     *
     * @param ids 单元信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteUnitss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的单元信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    /**
     * 根据教材ID查询单元ID列表
     *
     * @param programId 教材ID
     * @return 单元ID列表
     */
    @Override
    public List<Long> getUnitIdsByProgramId(Long programId) {
        return this.baseMapper.getUnitIdsByProgramId(programId);
    }

}
